package com.joyoung.sweep.utils;

import android.content.Context;

import com.joyoung.sdk.config.Cons_Sdk;
import com.joyoung.sdk.utils.encryptdecryptutil.XXTea;
import com.joyoung.sweep.config.Cons;
import com.joyoung.sweep.model.BaseResult;
import com.joyoung.sweep.model.JyUser;
import com.joyoung.sweep.model.UserConfirmData;

import org.apaches.commons.codec.DecoderException;
import org.apaches.commons.codec.binary.Hex;


public class User {

    /**
     * 检查上次登录用户是否存在,如果存在，则保存到变量区域，并返回true
     */
    public static boolean checkOldUser(Context context) {
        long loginTime = (long) SPUtils.get(context, Cons.KEY_LOGINTIME, 0L);
        loginTime = System.currentTimeMillis() - loginTime;
        loginTime = loginTime / 1000 / 60 / 60 / 24;
        if (loginTime > 7) {
            return false;
        }
        try {
            JyUser user = (JyUser) SPUtils.getObject(context, Cons.FILENAME);
            if (user != null && user.getSourcetype() != null && !user.getSourcetype()
                    .equalsIgnoreCase("-1")) {
                Cons.user = user;
                Cons.sessionKey = user.getSessionKey();
                Cons_Sdk.sessionId = Cons.sessionKey;
                Cons_Sdk.dataKey = user.getDataKey();
                Cons_Sdk.sessionkey = user.getSessionId();
                if (Cons_Sdk.dataKey == null || Cons_Sdk.dataKey.equals("")) {
                    return false;
                }
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 保存登录用户
     */
    public static void saveLoginInfo(Object obj, Context context) {
        SPUtils.saveOAuth(context, Cons.FILENAME, obj);
    }


    /**
     * 注销登录
     */
    public static void delUser(Context context) {
        SPUtils.remove(context, Cons.FILENAME);
    }


    public static void saveLoginUser(Context context, BaseResult<UserConfirmData> result,
                                     String xxteaKey) {
        SPUtils.put(context, Cons.KEY_LOGINTIME, System.currentTimeMillis());
        Cons.sessionKey = result.getData().getSession_id();
        SPUtils.put(context, Cons.SP_SESSION_KEY, Cons.sessionKey);
        Cons_Sdk.sessionId = result.getData().getSession_id();
        result.getData().getUser().setSessionKey(Cons.sessionKey);
        //通过accessToken解密sessionkey
        byte[] accessToken2 = new byte[0];
        try {
            accessToken2 = Hex.decodeHex(result.getData().getAccess_token().toCharArray());
        } catch (DecoderException e) {
            e.printStackTrace();
        }
        Cons_Sdk.sessionkey = new String(XXTea.decrypt(accessToken2, xxteaKey.getBytes()));
        StringBuilder dataKey = new StringBuilder();
        for (int i = 0; i < Cons_Sdk.sessionkey.length(); i++) {
            if (i % 2 == 0) {
                dataKey.append(Cons_Sdk.sessionkey.substring(i, i + 1));
            }
        }
        Cons_Sdk.dataKey = dataKey.toString();
        result.getData().getUser().setSessionId(Cons_Sdk.sessionkey);
        result.getData().getUser().setDataKey(Cons_Sdk.dataKey);
        Cons.user = result.getData().getUser();
        User.saveLoginInfo(Cons.user, context);
       // JYUmRecStatistics.Companion.userLogin(Cons.user.getId());
    }
}
